package com.tjc.junit;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
import java.util.stream.Collectors;

public class TestCase {


    public void run() {
        List<Method> testMethods = scanTestMethod();
        invokCase(testMethods);
    }

    private List<Method> scanTestMethod() {
        Method[] methods = this.getClass().getMethods();
        return Arrays.stream(methods).filter(m -> m.getName().startsWith("test")).collect(Collectors.toList());
    }

    private void invokCase(List<Method> testMethods) {
            testMethods.stream().forEach(m -> {
                try {
                    setUp();
                    m.invoke(this);
                    tearDown();
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                } catch (InvocationTargetException e) {
                    e.printStackTrace();
                }
            });
    }


    public void setUp(){}

    public void tearDown(){}

}
